home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.lang.c.moderated
- Subject: Re: Can someone tell me why C can't support the following
- Date: 17 Feb 1996 12:04:15 -0600
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4g55av$asn@solutions.solon.com>
- References: <4g27n5$qa4@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4g27n5$qa4@solutions.solon.com>,
- Alan Krantz <atk@mathcs.emory.edu> wrote:
- >
- >I know the following is illegal but I would really really like to know
- >why C can't support the following construct:
- >
- >typedef struct _mytbl {
-
- This begins a type declaration. So far so good...
-
- > char *str; ; Leagal
- > int *data; ; Illegal (see below)
-
- Nothing illegal about a char * structure member.
-
- >} mytbl[] = {"Not Defined", {0},
- > "Square", {1,2,3,4,0},
- > "Circle", {1,2,0},
-
- oops! Now you are declaring storage. You can't declare storage inside a type
- definition!
-
- >};
- >
- >Note1: I want data to point to an array of integer who's length is determined
- >by a {} list. I could do something silly like "int data[10]" and assume all
- >lists have 10 or fewer elements.
- >
- >NOTE2: I am NOT asking the compiler to generate a variable size structure
-
- You sure aren't. You are asking the compiler to generate an error. What you
- have is not a C construct.
-
-
- >called mytbl where data varies in size. I AM asking it to allocate a vector
- >of integer and assign the address of that integer to data. Wouldn't this be
- >a nice feature....
-
- The C language has such a feature. I have _statically_ declared entire _trees_!
-
- The correct way to do it is to remove the typedef. Either that, or do the
- typedef like this:
-
- typedef struct _mytbl {
- char *str;
- int *data;
- } mytbl_type;
-
- int p[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; /* vector of integers */
-
- mytbl_type mytbl[] = {
- { "First Prime", &p[0] }, /* string, address of integer */
- { "Second Prime", &p[1] },
- { "Third Prime", &p[3] },
- /* ... */
- { "Ninth Prime", &p[8] },
- };
-
- This brings us to the real point of your question. Wouldn't it be nice if we
- could do:
-
- int *x = 3; ?
-
- In short, no---it wouldn't buy you anything. It is allowed for string literals
- because C variables are defined in terms of storage units, and no atomic
- variable consists of enough storage cells to hold arbitrary strings. Thus we
- have a mechanism to create an unnamed piece of storage and statically bind it
- to a pointer. Doing so for integer literals would not be very useful.
-
- Even if it were (and there are arguably many useful things that C _could_
- have), it would be difficult to draft into existing standards.
-
-
-
-
-
-
-
-
-
- >Well send reasons why this is bad (and flames) to atk@cs.colorado.edu since
- >I don't read the news group!
-
- Then don't write to it.
- --
-